/**
 * Copyright (c) 2012 Nokia Corporation. 
 */

package com.nokia.example.tictactoe.ui;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;

import com.nokia.example.tictactoe.Main;
import com.nokia.example.tictactoe.engine.GameModel;

/**
 * Settings view.
 */
public class SettingsView extends Form {
	// Constants
	private static final int MAX_GRID_SIZE_SQUARED = 8;
	private static final int MIN_MARKS_TO_WIN = 3;
	
	// Members
	private Main _midlet = null;
	private GameModel _gameModel = null;
	private Gauge _gridSizeControl = null;
	private Gauge _marksRequiredToWinControl = null;
	private ChoiceGroup _showConsoleControl = null;
	private final Command _applySettingsCommand =
			new Command("Save", Command.OK, 0x01);
	private int _gridSizeSquared = 0;
	private int _marksRequiredToWin = 0;
	private int _gridSizeSquaredBefore = 0;
	private int _marksRequiredToWinBefore = 0;
	private boolean _showConsoleBefore = false;
	
	/**
	 * Constructor.
	 * @param title
	 */
	public SettingsView(String title, Main main) {
		super(title);
		_midlet = main;
		setGameModel(GameModel.instance(null));
		addCommand(_applySettingsCommand);
	}
	
	/**
	 * Sets the game model and initializes the setting controls based on the
	 * current settings.
	 * @param gameModel The game model.
	 */
	public final void setGameModel(final GameModel gameModel) {
		if (_gameModel != null) {
			// Already set
			return;
		}
		
		_gameModel = gameModel;
		
		if (_gameModel != null) {
			_gridSizeSquared = _gameModel.gridSizeSquared();
			_marksRequiredToWin = _gameModel.marksRequiredToWin();
			_gridSizeSquaredBefore = _gridSizeSquared;
			_marksRequiredToWinBefore = _marksRequiredToWin;
			init();
		}
	}
	
	/**
	 * Applies the current settings.
	 */
	public final boolean applySettings() {
		_gridSizeSquared = _gridSizeControl.getValue();
		_marksRequiredToWin = _marksRequiredToWinControl.getValue();
		
		System.out.println("SettingsView::applySettings(): "
				+ _gridSizeSquared + ", " + _marksRequiredToWin);
		
		if (settingsChanged()) {
			if (_gridSizeSquared < MIN_MARKS_TO_WIN
					|| _marksRequiredToWin > _gridSizeSquared)
			{
				// Invalid values
				String errorMsg = null;
				
				if (_gridSizeSquared < MIN_MARKS_TO_WIN) {
					errorMsg = "Minimum value of grid size squared is "
							+ MIN_MARKS_TO_WIN;
				}
				else {
					errorMsg = "The number of marks required to win has to be "
							+ "same or less than grid size squared";
				}
				
				Alert alert = null;
				
				try {
					alert = (Alert)Display.getDisplay(_midlet).getCurrent();
				}
				catch (Exception e) {
					alert = new Alert("Check values", errorMsg, null, AlertType.ERROR);
					Display.getDisplay(_midlet).setCurrent(alert);
				}
				
				return false;
			}
			
			_gameModel.setSpecs(_gridSizeSquared, _marksRequiredToWin);
		}
		else {
			System.out.println("SettingsView::applySettings(): No changes made.");
		}
		
		return true;
	}
	
	/** 
	 * @return True if the settings in the view differ from the ones in the
	 * 		   game model. False otherwise.
	 */
	public boolean settingsChanged() {
		_gridSizeSquared = _gridSizeControl.getValue();
		_marksRequiredToWin = _marksRequiredToWinControl.getValue();
		
		if (_gridSizeSquared != _gridSizeSquaredBefore
				|| _marksRequiredToWin != _marksRequiredToWinBefore
				|| _showConsoleControl.isSelected(0) != _showConsoleBefore)
		{
			return true;
		}

		return false;
	}
	
	public void setShowConsole(boolean show) {
		if (_showConsoleControl != null) {
			_showConsoleControl.setSelectedIndex(0, show);
			_showConsoleBefore = show;
		}
	}
	
	public boolean showConsole() {
		if (_showConsoleControl != null) {
			return _showConsoleControl.isSelected(0);
		}

		return false;
	}
	
	/**
	 * Creates the components of the view.
	 */
	private final void init() {
		_gridSizeControl =
				new Gauge("Grid size (squared)", true, 
						MAX_GRID_SIZE_SQUARED, _gridSizeSquared);
		_marksRequiredToWinControl =
				new Gauge("Marks required to win", true,
						MAX_GRID_SIZE_SQUARED, _marksRequiredToWin);
		_showConsoleControl =
				new ChoiceGroup("Console", ChoiceGroup.MULTIPLE,
								new String[] { "Show console" }, null);
		append(_gridSizeControl);
		append(_marksRequiredToWinControl);
		append(_showConsoleControl);
	}
}
